package com.example.utils;

/**
 * 通用接口返回工具
 */
public class Result {
	
	private boolean flag;
	private Integer code;
	private String message;
	private Object data;

	public Result() {
	}
	
	public Result(boolean flag, Integer code, String message) {
		super();
		this.flag = flag;
		this.code = code;
		this.message = message;
	}

	public <T> Result(boolean flag, Integer code, String message, T data) {
		super();
		this.flag = flag;
		this.code = code;
		this.message = message;
		this.data = data;
	}

	public boolean isFlag() {
		return flag;
	}
	public Result setFlag(boolean flag) {
		this.flag = flag;
		return this;
	}
	public Integer getCode() {
		return code;
	}
	public Result setCode(Integer code) {
		this.code = code;
		return this;
	}
	public String getMessage() {
		return message;
	}
	public Result setMessage(String message) {
		this.message = message;
		return this;
	}
	public <T> T getData() {
		return (T) data;
	}
	public <T> Result setData(T data) {
		this.data = data;
		return this;
	}

	/**
	 * 返回成功消息
	 * @return Result
	 */
	public static Result ok() {
		return new Result(true, ResultCode.OK.getCode(), ResultCode.OK.getMsg());
	}
	
	/**
	 * 返回成功消息
	 * @return Result
	 */
	public static Result ok(Object data) {
		return new Result(true, ResultCode.OK.getCode(), ResultCode.OK.getMsg(), data);
	}

	/**
	 * 返回失败消息
	 * @return Result
	 */
	public static Result error() {
		return new Result(false, ResultCode.ERROR.getCode(), ResultCode.ERROR.getMsg());
	}
	
	/**
	 * 返回失败消息
	 * @return Result
	 */
	public static Result error(String message) {
		return new Result(false, ResultCode.ERROR.getCode(), message);
	}
	
	/**
	 * 返回失败消息
	 * @return Result
	 */
	public static Result error(Integer code, String message) {
		return new Result(false, code, message);
	}
	
	@Override
	public String toString() {
		return "Result [flag=" + flag + ", code=" + code + ", message=" + message + ", data=" + data + "]";
	}
}
